<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Docker 学习 | 仲灏小栈</title>
    <meta name="generator" content="VuePress 1.8.0">
    <link rel="icon" href="/img/favicon.ico">
    <meta name="description" content="专注web全栈学习与总结。JavaScript,js,ES6,TypeScript,vue,python,css3,html5,Node,git,github等技术文章。">
    <meta name="keywords" content="前端博客,个人技术博客,前端,前端开发,前端框架,web前端,前端面试题,技术文档,学习,面试,JavaScript,js,ES6,TypeScript,vue,python,css3,html5,Node,git,github,markdown">
    <meta name="baidu-site-verification" content="code-5CGlqw1B4p">
    <meta name="google-site-verification" content="PbO8RiLRwDnNwqNFNGnaxZnEFB3CqcGMCGoloYfQXV0">
    <meta name="theme-color" content="#11a8cd">
    
    <link rel="preload" href="/assets/css/0.styles.15972595.css" as="style"><link rel="preload" href="/assets/js/app.6e09c0e4.js" as="script"><link rel="preload" href="/assets/js/2.26fadcea.js" as="script"><link rel="preload" href="/assets/js/71.39859d24.js" as="script"><link rel="prefetch" href="/assets/js/10.29078de5.js"><link rel="prefetch" href="/assets/js/100.1775343f.js"><link rel="prefetch" href="/assets/js/101.03c2a233.js"><link rel="prefetch" href="/assets/js/102.2d566277.js"><link rel="prefetch" href="/assets/js/103.28f0aae6.js"><link rel="prefetch" href="/assets/js/104.98d074e6.js"><link rel="prefetch" href="/assets/js/105.a113c54b.js"><link rel="prefetch" href="/assets/js/106.fe975924.js"><link rel="prefetch" href="/assets/js/107.06b48459.js"><link rel="prefetch" href="/assets/js/108.650cfa65.js"><link rel="prefetch" href="/assets/js/109.8dea05a5.js"><link rel="prefetch" href="/assets/js/11.0ffc15c2.js"><link rel="prefetch" href="/assets/js/110.f426e949.js"><link rel="prefetch" href="/assets/js/111.cb0ff462.js"><link rel="prefetch" href="/assets/js/112.2234a7ce.js"><link rel="prefetch" href="/assets/js/113.d42a4afb.js"><link rel="prefetch" href="/assets/js/114.afa6c93d.js"><link rel="prefetch" href="/assets/js/115.e74e58c4.js"><link rel="prefetch" href="/assets/js/116.b52912dc.js"><link rel="prefetch" href="/assets/js/117.f5e176df.js"><link rel="prefetch" href="/assets/js/118.428fdb24.js"><link rel="prefetch" href="/assets/js/119.c7b3c702.js"><link rel="prefetch" href="/assets/js/12.5358dc27.js"><link rel="prefetch" href="/assets/js/120.b28cb8e3.js"><link rel="prefetch" href="/assets/js/121.517216d1.js"><link rel="prefetch" href="/assets/js/122.224b318f.js"><link rel="prefetch" href="/assets/js/123.04e6a56c.js"><link rel="prefetch" href="/assets/js/13.1eb6db46.js"><link rel="prefetch" href="/assets/js/14.2d919975.js"><link rel="prefetch" href="/assets/js/15.351e3a4c.js"><link rel="prefetch" href="/assets/js/16.3acd2d41.js"><link rel="prefetch" href="/assets/js/17.60b03995.js"><link rel="prefetch" href="/assets/js/18.c6ad362d.js"><link rel="prefetch" href="/assets/js/19.8ab677ff.js"><link rel="prefetch" href="/assets/js/20.99f99fea.js"><link rel="prefetch" href="/assets/js/21.c1d6f733.js"><link rel="prefetch" href="/assets/js/22.6f0b0c91.js"><link rel="prefetch" href="/assets/js/23.13368cdd.js"><link rel="prefetch" href="/assets/js/24.c53a9ca6.js"><link rel="prefetch" href="/assets/js/25.cd4407c5.js"><link rel="prefetch" href="/assets/js/26.d9b2ef17.js"><link rel="prefetch" href="/assets/js/27.16352eff.js"><link rel="prefetch" href="/assets/js/28.a62c7d25.js"><link rel="prefetch" href="/assets/js/29.635009f5.js"><link rel="prefetch" href="/assets/js/3.72d46b47.js"><link rel="prefetch" href="/assets/js/30.db885055.js"><link rel="prefetch" href="/assets/js/31.aa8c6cb9.js"><link rel="prefetch" href="/assets/js/32.e63d710c.js"><link rel="prefetch" href="/assets/js/33.4f69bb69.js"><link rel="prefetch" href="/assets/js/34.9d242b1d.js"><link rel="prefetch" href="/assets/js/35.24c7045d.js"><link rel="prefetch" href="/assets/js/36.2988cd72.js"><link rel="prefetch" href="/assets/js/37.05bf63d5.js"><link rel="prefetch" href="/assets/js/38.ecef1ea2.js"><link rel="prefetch" href="/assets/js/39.0fde8ff2.js"><link rel="prefetch" href="/assets/js/4.d95386e0.js"><link rel="prefetch" href="/assets/js/40.99489203.js"><link rel="prefetch" href="/assets/js/41.928918ed.js"><link rel="prefetch" href="/assets/js/42.8ff04c77.js"><link rel="prefetch" href="/assets/js/43.347198e5.js"><link rel="prefetch" href="/assets/js/44.fc4d4540.js"><link rel="prefetch" href="/assets/js/45.5e4eff92.js"><link rel="prefetch" href="/assets/js/46.83a0c228.js"><link rel="prefetch" href="/assets/js/47.37c2f407.js"><link rel="prefetch" href="/assets/js/48.bada4ddc.js"><link rel="prefetch" href="/assets/js/49.562d8b65.js"><link rel="prefetch" href="/assets/js/5.77e36999.js"><link rel="prefetch" href="/assets/js/50.338be56f.js"><link rel="prefetch" href="/assets/js/51.999e3de3.js"><link rel="prefetch" href="/assets/js/52.11309761.js"><link rel="prefetch" href="/assets/js/53.ea6a0c8a.js"><link rel="prefetch" href="/assets/js/54.a49ee06c.js"><link rel="prefetch" href="/assets/js/55.d4d985f2.js"><link rel="prefetch" href="/assets/js/56.258972e6.js"><link rel="prefetch" href="/assets/js/57.e7d3b5af.js"><link rel="prefetch" href="/assets/js/58.50c14a49.js"><link rel="prefetch" href="/assets/js/59.8edb599b.js"><link rel="prefetch" href="/assets/js/6.ba658368.js"><link rel="prefetch" href="/assets/js/60.d1250584.js"><link rel="prefetch" href="/assets/js/61.28187287.js"><link rel="prefetch" href="/assets/js/62.1cb42759.js"><link rel="prefetch" href="/assets/js/63.3fd80d10.js"><link rel="prefetch" href="/assets/js/64.9f43af49.js"><link rel="prefetch" href="/assets/js/65.c4a47594.js"><link rel="prefetch" href="/assets/js/66.cd22b5d7.js"><link rel="prefetch" href="/assets/js/67.5c9918c9.js"><link rel="prefetch" href="/assets/js/68.fb876377.js"><link rel="prefetch" href="/assets/js/69.3414f481.js"><link rel="prefetch" href="/assets/js/7.83e78719.js"><link rel="prefetch" href="/assets/js/70.f003a000.js"><link rel="prefetch" href="/assets/js/72.12912986.js"><link rel="prefetch" href="/assets/js/73.abd327ea.js"><link rel="prefetch" href="/assets/js/74.36cb7d1d.js"><link rel="prefetch" href="/assets/js/75.1cb96b9a.js"><link rel="prefetch" href="/assets/js/76.6033c6da.js"><link rel="prefetch" href="/assets/js/77.083624e2.js"><link rel="prefetch" href="/assets/js/78.70d3273c.js"><link rel="prefetch" href="/assets/js/79.14f3aecd.js"><link rel="prefetch" href="/assets/js/8.bcb11f96.js"><link rel="prefetch" href="/assets/js/80.95205bda.js"><link rel="prefetch" href="/assets/js/81.a39e63f0.js"><link rel="prefetch" href="/assets/js/82.6298e8dc.js"><link rel="prefetch" href="/assets/js/83.109c5547.js"><link rel="prefetch" href="/assets/js/84.6a1959a1.js"><link rel="prefetch" href="/assets/js/85.6ff6f61a.js"><link rel="prefetch" href="/assets/js/86.7d2bfbe7.js"><link rel="prefetch" href="/assets/js/87.2032d6ae.js"><link rel="prefetch" href="/assets/js/88.5cda9aa6.js"><link rel="prefetch" href="/assets/js/89.60c9e091.js"><link rel="prefetch" href="/assets/js/9.170a94b1.js"><link rel="prefetch" href="/assets/js/90.0dab9f57.js"><link rel="prefetch" href="/assets/js/91.ecf75d5a.js"><link rel="prefetch" href="/assets/js/92.74397fcb.js"><link rel="prefetch" href="/assets/js/93.f5559b26.js"><link rel="prefetch" href="/assets/js/94.27c65783.js"><link rel="prefetch" href="/assets/js/95.ea55ba1f.js"><link rel="prefetch" href="/assets/js/96.8a68510f.js"><link rel="prefetch" href="/assets/js/97.ef520b02.js"><link rel="prefetch" href="/assets/js/98.d4a41570.js"><link rel="prefetch" href="/assets/js/99.bdd726ad.js">
    <link rel="stylesheet" href="/assets/css/0.styles.15972595.css">
  </head>
  <body class="theme-mode-light">
    <div id="app" data-server-rendered="true"><div class="theme-container sidebar-open have-rightmenu have-body-img"><header class="navbar blur"><div title="目录" class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/img/logo.png" alt="仲灏小栈" class="logo"> <span class="site-name can-hide">仲灏小栈</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><a href="/web/" class="nav-link">大前端</a></div><div class="nav-item"><a href="/backend-opt/" class="nav-link">后端&amp;运维</a></div><div class="nav-item"><a href="/other-tech/" class="nav-link">其他</a></div><div class="nav-item"><a href="/tech-live/" class="nav-link">生活</a></div><div class="nav-item"><a href="/about/" class="nav-link">关于我</a></div><div class="nav-item"><a href="/favorite/" class="nav-link">收藏</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div> <a href="https://github.com/izhaong" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar-hover-trigger"></div> <aside class="sidebar" style="display:none;"><div class="blogger"><img src="https://cdn.jsdelivr.net/gh/izhaong/izhaong.com-oss/site-data/avatar/avatar-wx.jpg"> <div class="blogger-info"><h3>仲灏</h3> <span>诚意, 正心, 格物, 致知</span></div></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><a href="/web/" class="nav-link">大前端</a></div><div class="nav-item"><a href="/backend-opt/" class="nav-link">后端&amp;运维</a></div><div class="nav-item"><a href="/other-tech/" class="nav-link">其他</a></div><div class="nav-item"><a href="/tech-live/" class="nav-link">生活</a></div><div class="nav-item"><a href="/about/" class="nav-link">关于我</a></div><div class="nav-item"><a href="/favorite/" class="nav-link">收藏</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div> <a href="https://github.com/izhaong" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Java</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>Docker</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/pages/3eb92e11/" aria-current="page" class="active sidebar-link">Docker 学习</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/pages/3eb92e11/#docker-安装及使用教程" class="sidebar-link">Docker 安装及使用教程</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/pages/3eb92e11/#卸载-docker" class="sidebar-link">卸载 docker</a></li><li class="sidebar-sub-header"><a href="/pages/3eb92e11/#安装-docker" class="sidebar-link">安装 docker</a></li><li class="sidebar-sub-header"><a href="/pages/3eb92e11/#docker-镜像加速" class="sidebar-link">docker 镜像加速</a></li></ul></li><li class="sidebar-sub-header"><a href="/pages/3eb92e11/#docker-入门教程" class="sidebar-link">Docker 入门教程</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/pages/3eb92e11/#image-文件" class="sidebar-link">image 文件</a></li><li class="sidebar-sub-header"><a href="/pages/3eb92e11/#容器文件" class="sidebar-link">容器文件</a></li><li class="sidebar-sub-header"><a href="/pages/3eb92e11/#实例" class="sidebar-link">实例</a></li><li class="sidebar-sub-header"><a href="/pages/3eb92e11/#dockerfile" class="sidebar-link">dockerFile</a></li></ul></li><li class="sidebar-sub-header"><a href="/pages/3eb92e11/#gui-工具" class="sidebar-link">GUI 工具</a></li><li class="sidebar-sub-header"><a href="/pages/3eb92e11/#常用命令汇总" class="sidebar-link">常用命令汇总</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/pages/3eb92e11/#命令行技巧" class="sidebar-link">命令行技巧</a></li></ul></li><li class="sidebar-sub-header"><a href="/pages/3eb92e11/#容器地址" class="sidebar-link">容器地址</a></li><li class="sidebar-sub-header"><a href="/pages/3eb92e11/#" class="sidebar-link"></a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/pages/3eb92e11/#-2" class="sidebar-link"></a></li></ul></li><li class="sidebar-sub-header"><a href="/pages/3eb92e11/#原理" class="sidebar-link">原理</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/pages/3eb92e11/#cnm" class="sidebar-link">CNM</a></li><li class="sidebar-sub-header"><a href="/pages/3eb92e11/#容器内部网络空间是否与外面一致" class="sidebar-link">容器内部网络空间是否与外面一致</a></li><li class="sidebar-sub-header"><a href="/pages/3eb92e11/#-3" class="sidebar-link"></a></li></ul></li><li class="sidebar-sub-header"><a href="/pages/3eb92e11/#-4" class="sidebar-link"></a></li><li class="sidebar-sub-header"><a href="/pages/3eb92e11/#docker-微服务" class="sidebar-link">docker 微服务</a></li><li class="sidebar-sub-header"><a href="/pages/3eb92e11/#-5" class="sidebar-link"></a></li><li class="sidebar-sub-header"><a href="/pages/3eb92e11/#nginx-容器教程" class="sidebar-link">Nginx 容器教程</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/pages/3eb92e11/#http-服务" class="sidebar-link">HTTP 服务</a></li></ul></li><li class="sidebar-sub-header"><a href="/pages/3eb92e11/#映射网页目录" class="sidebar-link">映射网页目录</a></li><li class="sidebar-sub-header"><a href="/pages/3eb92e11/#使用的案例" class="sidebar-link">使用的案例</a></li><li class="sidebar-sub-header"><a href="/pages/3eb92e11/#在使用-docker-中遇到的问题-需求" class="sidebar-link">在使用 docker 中遇到的问题&amp;需求</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/pages/3eb92e11/#问题" class="sidebar-link">问题</a></li><li class="sidebar-sub-header"><a href="/pages/3eb92e11/#需求收集" class="sidebar-link">需求收集</a></li></ul></li></ul></li><li><a href="/pages/daf659111/" class="sidebar-link">Docker docker-compose笔记</a></li><li><a href="/pages/2e1376/" class="sidebar-link">Docker 配置详解</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Nacos</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>数据库</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Nginx</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Linux</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Git</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>其他</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <div><main class="page"><div class="theme-vdoing-wrapper "><div class="articleInfo-wrap" data-v-0c557b5e><div class="articleInfo" data-v-0c557b5e><ul class="breadcrumbs" data-v-0c557b5e><li data-v-0c557b5e><a href="/" title="首页" class="iconfont icon-home router-link-active" data-v-0c557b5e></a></li> <li data-v-0c557b5e><a href="/backend-opt/#后端&amp;运维" data-v-0c557b5e>后端&amp;运维</a></li><li data-v-0c557b5e><a href="/backend-opt/#Docker" data-v-0c557b5e>Docker</a></li></ul> <div class="info" data-v-0c557b5e><div title="作者" class="author iconfont icon-touxiang" data-v-0c557b5e><a href="https://github.com/izhaong" target="_blank" title="作者" class="beLink" data-v-0c557b5e>仲灏</a></div> <div title="创建时间" class="date iconfont icon-riqi" data-v-0c557b5e><a href="javascript:;" data-v-0c557b5e>2021-11-30</a></div> <!----></div></div></div> <!----> <div class="content-wrapper"><div class="right-menu-wrapper"><div class="right-menu-margin"><div class="right-menu-content"></div></div></div> <h1><img src="">Docker 学习<!----></h1> <!----> <div class="theme-vdoing-content content__default"><p><em>前言: 因为写项目时需要用到<code>mysql</code> <code>postgres``mongodb``redis</code>一些数据库 不想再自己电脑上安装这些 使用率不是很多 就想到了 docker, 无论在服务器还时自己的电脑上, 感觉 docker 更符合我的场景</em></p> <h2 id="docker-安装及使用教程"><a href="#docker-安装及使用教程" class="header-anchor">#</a> <a href="https://juejin.im/post/6844904201990963213" target="_blank" rel="noopener noreferrer">Docker 安装及使用教程<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></h2> <h3 id="卸载-docker"><a href="#卸载-docker" class="header-anchor">#</a> 卸载 docker</h3> <p>1.  先查询下 docker： yum list installed|grep docker ，然后执行卸载命令：<code>yum -y remove docker.x86_64 docker-client.x86_64 docker-common.x86_64</code>
2.  执行删除已存在的镜像和容器（以实际情况而定）： <code>rm -rf /var/lib/docker</code></p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment"># 删除相关组件</span>
<span class="token comment"># 如果使用种方式的话，需要罗列全部使安装过的组件；</span>
yum remove docker <span class="token punctuation">\</span>
  docker-client <span class="token punctuation">\</span>
  docker-client-latest <span class="token punctuation">\</span>
  docker-common <span class="token punctuation">\</span>
  docker-latest <span class="token punctuation">\</span>
  docker-latest-logrotate <span class="token punctuation">\</span>
  docker-logrotate <span class="token punctuation">\</span>
  docker-selinux <span class="token punctuation">\</span>
  docker-engine-selinux <span class="token punctuation">\</span>
  docker-engine

<span class="token comment"># 第二种方式，使用匹配符直接删除全部</span>
<span class="token comment"># 推荐使用第二种方式，无需罗列所有的安装组件</span>
yum remove docker docker-*
<span class="token comment"># 删除注册的服务</span>
<span class="token function">rm</span> -rf /etc/systemd/system/docker.service.d
<span class="token comment"># 删除docker工作目录</span>
<span class="token function">rm</span> -rf /var/lib/docker*
<span class="token function">rm</span> -rf /var/run/docker*
<span class="token comment"># 删除配置文件</span>
<span class="token comment"># 直接清空这个文件夹，下面所有配置都将被清空</span>
<span class="token function">rm</span> -rf /etc/docker
<span class="token comment">#----------------------------#</span>
<span class="token comment"># /etc/docker/daemon.json    #</span>
<span class="token comment"># /ect/docker/key.json       #</span>
<span class="token comment">#----------------------------#</span>
<span class="token comment"># 查找残留的docker</span>
<span class="token function">rpm</span> -qa<span class="token operator">|</span><span class="token function">grep</span> docker
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br></div></div><h3 id="安装-docker"><a href="#安装-docker" class="header-anchor">#</a> 安装 docker</h3> <div class="language- line-numbers-mode"><pre class="language-text"><code># 第一步，需要安装一些依赖组件
# 安装所需的软件包。yum-utils 提供了 yum-config-manager，
# 并且 device mapper 存储驱动程序需要 device-mapper-persistent-data 和 lvm2。
yum install -y yum-utils \
  device-mapper-persistent-data \
  lvm2
# 使用以下命令来获得稳定的仓库
# 阿里云源地址速度相对比较快，这里是设置源地址为阿里云的镜像仓库地址。
# yum-config-manager \
#    --add-repo \
#    http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

# 安装 Docker Engine-Community
yum install -y docker-ce docker-ce-cli containerd.io
# 将当前用户添加到docker用户组
usermod -aG docker ${USER}
# Docker默认未启动，需要手动重启docker服务
systemctl restart docker
systemctl start docker
systemctl stop docker
# 切换当前会话到docker组
newgrp - docker
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br></div></div><h3 id="docker-镜像加速"><a href="#docker-镜像加速" class="header-anchor">#</a> docker 镜像加速</h3> <div class="language- line-numbers-mode"><pre class="language-text"><code>sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json &lt;&lt;-'EOF'
{
  &quot;registry-mirrors&quot;: [ &quot;https://fpr97nhz.mirror.aliyuncs.com&quot; ]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h2 id="docker-入门教程"><a href="#docker-入门教程" class="header-anchor">#</a> <a href="http://www.ruanyifeng.com/blog/2018/02/docker-tutorial.html" target="_blank" rel="noopener noreferrer">Docker 入门教程<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></h2> <h3 id="image-文件"><a href="#image-文件" class="header-anchor">#</a> image 文件</h3> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment"># 列出本机的所有 image 文件。</span>
$ docker image <span class="token function">ls</span>

<span class="token comment"># 删除 image 文件</span>
$ docker image <span class="token function">rm</span> <span class="token punctuation">[</span>imageName<span class="token punctuation">]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="容器文件"><a href="#容器文件" class="header-anchor">#</a> 容器文件</h3> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment"># 列出本机正在运行的容器</span>
$ docker container <span class="token function">ls</span>

<span class="token comment"># 列出本机所有容器，包括终止运行的容器</span>
$ docker container <span class="token function">ls</span> --all
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>终止运行的容器文件，依然会占据硬盘空间，可以使用<a href="https://docs.docker.com/engine/reference/commandline/container_rm/" target="_blank" rel="noopener noreferrer"><code>docker container rm</code><span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>命令删除。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>$ docker container rm [containerID]
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="实例"><a href="#实例" class="header-anchor">#</a> 实例</h3> <p>首先，运行下面的命令，将 image 文件从仓库抓取到本地。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>$ docker image pull library/hello-world
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>上面代码中，<code>docker image pull</code>是抓取 image 文件的命令。<code>library/hello-world</code>是 image 文件在仓库里面的位置，其中<code>library</code>是 image 文件所在的组，<code>hello-world</code>是 image 文件的名字。
由于 Docker 官方提供的 image 文件，都放在<a href="https://hub.docker.com/r/library/" target="_blank" rel="noopener noreferrer"><code>library</code><span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>组里面，所以它的是默认组，可以省略。因此，上面的命令可以写成下面这样。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>$ docker image pull hello-world
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>抓取成功以后，就可以在本机看到这个 image 文件了。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>$ docker image ls
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>现在，运行这个 image 文件。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>$ docker container run hello-world
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><code>docker container run</code>命令会从 image 文件，生成一个正在运行的容器实例。
注意，<code>docker container run</code>命令具有自动抓取 image 文件的功能。如果发现本地没有指定的 image 文件，就会从仓库自动抓取。因此，前面的<code>docker image pull</code>命令并不是必需的步骤。
如果运行成功，你会在屏幕上读到下面的输出。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>$ docker container run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.
... ...
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>输出这段提示以后，<code>hello world</code>就会停止运行，容器自动终止。
有些容器不会自动终止，因为提供的是服务。比如，安装运行 Ubuntu 的 image，就可以在命令行体验 Ubuntu 系统。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>$ docker container run -it ubuntu bash
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>对于那些不会自动终止的容器，必须使用<a href="https://docs.docker.com/engine/reference/commandline/container_kill/" target="_blank" rel="noopener noreferrer"><code>docker container kill</code><span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> 命令手动终止。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>$ docker container kill [containID]
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="dockerfile"><a href="#dockerfile" class="header-anchor">#</a> dockerFile</h3> <p><img src="https://cdn.nlark.com/yuque/0/2020/webp/714491/1602480106829-d4e077eb-849d-48e4-bd1a-0cd36e7d17da.webp#align=left&amp;display=inline&amp;height=296&amp;margin=%5Bobject%20Object%5D&amp;originHeight=296&amp;originWidth=563&amp;size=0&amp;status=done&amp;style=none&amp;width=563" alt=""></p> <h2 id="gui-工具"><a href="#gui-工具" class="header-anchor">#</a> GUI 工具</h2> <p>推荐使用 Portainer 作为容器的 GUI 管理方案。
推荐使用 Portainer 作为容器的 GUI 管理方案。</p> <blockquote><p>官方地址：<a href="https://links.jianshu.com/go?to=https%3A%2F%2Fportainer.io%2Finstall.html" target="_blank" rel="noopener noreferrer">portainer.io/install.htm…<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p></blockquote> <p>安装命令</p> <blockquote><p><code>docker volume create portainer_data</code> &gt; <code>docker run -d -p ``8000``:8000 -p ``9000``:9000 --name``=``portainer --restart``=``always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce</code></p></blockquote> <h2 id="常用命令汇总"><a href="#常用命令汇总" class="header-anchor">#</a> 常用命令汇总</h2> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment"># 查看docker基础信息</span>
docker info

<span class="token comment"># 拉取镜像</span>
docker pull <span class="token operator">&lt;</span>image<span class="token operator">&gt;</span>:<span class="token operator">&lt;</span>tag<span class="token operator">&gt;</span>

<span class="token comment"># 移除容器</span>
docker <span class="token function">rm</span> <span class="token operator">&lt;</span>containerId<span class="token operator">&gt;</span>

<span class="token comment"># 移除镜像</span>
docker rmi <span class="token operator">&lt;</span>imageId<span class="token operator">&gt;</span>

<span class="token comment"># 启动、停止、重启容器</span>
docker stop <span class="token operator">&lt;</span>containerId<span class="token operator">&gt;</span>
docker start <span class="token operator">&lt;</span>containerId<span class="token operator">&gt;</span>
docker restart <span class="token operator">&lt;</span>containerId<span class="token operator">&gt;</span>
docker run -itd --name <span class="token operator">&lt;</span>imageName<span class="token operator">&gt;</span> -P/-p<span class="token operator">&lt;</span>port<span class="token operator">&gt;</span>:<span class="token operator">&lt;</span>port<span class="token operator">&gt;</span> <span class="token operator">&lt;</span>imageName<span class="token operator">&gt;</span> /bin/bash

<span class="token comment"># 查看docker容器</span>
docker <span class="token function">ps</span> <span class="token operator">&lt;</span>-a<span class="token operator">&gt;</span>

<span class="token comment"># 打包镜像</span>
docker tag <span class="token operator">&lt;</span>path<span class="token operator">&gt;</span>/<span class="token operator">&lt;</span>name<span class="token operator">&gt;</span> <span class="token operator">&lt;</span>remotePath<span class="token operator">&gt;</span>/<span class="token operator">&lt;</span>name<span class="token operator">&gt;</span>
docker push <span class="token operator">&lt;</span>remotePath<span class="token operator">&gt;</span>/<span class="token operator">&lt;</span>name<span class="token operator">&gt;</span>

<span class="token comment"># 登录docker</span>
docker login <span class="token operator">&lt;</span>remoteIp<span class="token operator">&gt;</span>

<span class="token comment"># 进入容器内部</span>
docker <span class="token builtin class-name">exec</span> -it <span class="token operator">&lt;</span>containerId<span class="token operator">&gt;</span> /bin/bash

<span class="token comment"># 从仓库搜索镜像</span>
docker search <span class="token operator">&lt;</span>imageName<span class="token operator">&gt;</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br></div></div><p><strong>（1）docker container start</strong>
前面的<code>docker container run</code>命令是新建容器，每运行一次，就会新建一个容器。同样的命令运行两次，就会生成两个一模一样的容器文件。如果希望重复使用容器，就要使用<code>docker container start</code>命令，它用来启动已经生成、已经停止运行的容器文件。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>$ docker container start [containerID]
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><strong>（2）docker container stop</strong>
前面的<code>docker container kill</code>命令终止容器运行，相当于向容器里面的主进程发出 SIGKILL 信号。而<code>docker container stop</code>命令也是用来终止容器运行，相当于向容器里面的主进程发出 SIGTERM 信号，然后过一段时间再发出 SIGKILL 信号。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>$ bash container stop [containerID]
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>这两个信号的差别是，应用程序收到 SIGTERM 信号以后，可以自行进行收尾清理工作，但也可以不理会这个信号。如果收到 SIGKILL 信号，就会强行立即终止，那些正在进行中的操作会全部丢失。
<strong>（3）docker container logs</strong> <code>docker container logs</code>命令用来查看 docker 容器的输出，即容器里面 Shell 的标准输出。如果<code>docker run</code>命令运行容器的时候，没有使用<code>-it</code>参数，就要用这个命令查看输出。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>$ docker container logs [containerID]
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><strong>（4）docker container exec</strong> <code>docker container exec</code>命令用于进入一个正在运行的 docker 容器。如果<code>docker run</code>命令运行容器的时候，没有使用<code>-it</code>参数，就要用这个命令进入容器。一旦进入了容器，就可以在容器的 Shell 执行命令了。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>$ docker container exec -it [containerID] /bin/bash
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><code>exit</code> 容器停止退出
<code>crtl+p+q</code> 容器不停止退出
<strong>（5）docker container cp</strong> <code>docker container cp</code>命令用于从正在运行的 Docker 容器里面，将文件拷贝到本机。下面是拷贝到当前目录的写法。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>$ docker container cp [containID]:[/path/to/file] .
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="命令行技巧"><a href="#命令行技巧" class="header-anchor">#</a> 命令行技巧</h3> <h4 id="删除所有镜像"><a href="#删除所有镜像" class="header-anchor">#</a> 删除所有镜像</h4> <p>docker rmi -f $(docker images -qa)</p> <h4 id="停用全部运行中的容器"><a href="#停用全部运行中的容器" class="header-anchor">#</a> 停用全部运行中的容器</h4> <p>docker stop $(docker ps -q)</p> <h4 id="删除所有的容器"><a href="#删除所有的容器" class="header-anchor">#</a> 删除所有的容器</h4> <p>docker rm $(docker ps -aq)</p> <h4 id="停用并删除所有容器"><a href="#停用并删除所有容器" class="header-anchor">#</a> 停用并删除所有容器</h4> <p>docker stop $(docker ps -q) &amp; docker rm $(docker ps -aq)</p> <ul><li><p>删除所有为运行的容器 使用 linux 命令</p> <ul><li><code>docker container ls -f status-exited | awk '${print$1}' | awk 'NR&gt;1' | xargs docker container rm</code></li></ul></li> <li><p>批量启动容器 使用 docker 命令</p> <ul><li><code>docker container ls -q -f status=created</code> 查找所有状态为 created 的容器 并获取其 ID</li> <li><code>docker container start $(docker container ls -q -f status=created)</code></li></ul></li></ul> <h2 id="容器地址"><a href="#容器地址" class="header-anchor">#</a> 容器地址</h2> <p><code>inspect -f '{ {.Name}} - { {range .NetworkSettings.Networks} }{ {.IPAddress} }{ {end} }' $(docker ps -aq)</code></p> <p><img src="https://cdn.jsdelivr.net/gh/izhaong/izhaong.com-oss/blogImg/021001.Docker%20%E5%AD%A6%E4%B9%A0/2021/12/09/19-10-09-e2d13d2f6105006c72aed1048eb91c8e-image-20211209191006780-6d8ed9.png" alt="image-20211209191006780"></p> <h2 id=""><a href="#" class="header-anchor">#</a></h2> <h3 id="-2"><a href="#-2" class="header-anchor">#</a></h3> <h2 id="原理"><a href="#原理" class="header-anchor">#</a> 原理</h2> <h3 id="cnm"><a href="#cnm" class="header-anchor">#</a> CNM</h3> <p><img src="https://cdn.nlark.com/yuque/0/2020/png/714491/1604573908501-f5829082-6b0c-401a-82e5-66b557d479e8.png#align=left&amp;display=inline&amp;height=395&amp;margin=%5Bobject%20Object%5D&amp;name=image.png&amp;originHeight=395&amp;originWidth=721&amp;size=174736&amp;status=done&amp;style=none&amp;width=721" alt="image.png"> <strong>CNM Drivers</strong> <strong><img src="https://cdn.nlark.com/yuque/0/2020/png/714491/1604573952037-1c1dd9dd-3516-4e26-ab2c-d3c345cfb508.png#align=left&amp;display=inline&amp;height=370&amp;margin=%5Bobject%20Object%5D&amp;name=image.png&amp;originHeight=370&amp;originWidth=666&amp;size=143617&amp;status=done&amp;style=none&amp;width=666" alt="image.png"></strong></p> <h3 id="容器内部网络空间是否与外面一致"><a href="#容器内部网络空间是否与外面一致" class="header-anchor">#</a> 容器内部网络空间是否与外面一致</h3> <p>在容器外<code>ip a</code>
查看容器内所有<code>ip:``docker container exec ${containerName} ip a</code></p> <h3 id="-3"><a href="#-3" class="header-anchor">#</a></h3> <h2 id="-4"><a href="#-4" class="header-anchor">#</a></h2> <h2 id="docker-微服务"><a href="#docker-微服务" class="header-anchor">#</a> docker 微服务</h2> <h2 id="-5"><a href="#-5" class="header-anchor">#</a></h2> <hr> <h2 id="nginx-容器教程"><a href="#nginx-容器教程" class="header-anchor">#</a> <a href="https://www.yuque.com/izhaong/blog/pdeegh#Uyci8" target="_blank" rel="noopener noreferrer">Nginx 容器教程<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></h2> <h3 id="http-服务"><a href="#http-服务" class="header-anchor">#</a> HTTP 服务</h3> <p>Nginx 的最大作用，就是搭建一个 Web Server。有了容器，只要一行命令，服务器就架设好了，完全不用配置。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>$ docker container run \
  -d \
  -p 127.0.0.1:8080:80 \
  --rm \
  --name mynginx \
  nginx
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>上面命令下载并运行官方的 <a href="https://hub.docker.com/_/nginx/" target="_blank" rel="noopener noreferrer">Nginx image<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>，默认是最新版本（latest），当前是 1.13.9。如果本机安装过以前的版本，请删掉重新安装，因为只有 1.13.9 才开始支持 server push。
上面命令的各个参数含义如下。</p> <blockquote><ul><li><code>-d</code>：在后台运行</li> <li><code>-p</code> ：容器的<code>80</code>端口映射到<code>127.0.0.1:8080</code></li> <li><code>--rm</code>：容器停止运行后，自动删除容器文件</li> <li><code>--name</code>：容器的名字为<code>mynginx</code></li></ul></blockquote> <h2 id="映射网页目录"><a href="#映射网页目录" class="header-anchor">#</a> 映射网页目录</h2> <p>网页文件都在容器里，没法直接修改，显然很不方便。下一步就是让网页文件所在的目录<code>/usr/share/nginx/html</code>映射到本地。
首先，新建一个目录，并进入该目录。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>$ mkdir nginx-docker-demo
$ cd nginx-docker-demo
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>然后，新建一个<code>html</code>子目录。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>$ mkdir html
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>在这个子目录里面，放置一个<code>index.html</code>文件，内容如下。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>&lt;h1&gt;Hello World&lt;/h1&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>接着，就可以把这个子目录<code>html</code>，映射到容器的网页文件目录<code>/usr/share/nginx/html</code>。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>$ docker container run \
  -d \
  -p 127.0.0.1:8080:80 \
  --rm \
  --name mynginx \
  --volume &quot;$PWD/html&quot;:/usr/share/nginx/html \
  nginx
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>打开浏览器，访问 127.0.0.1:8080，应该就能看到 Hello World 了。</p> <h2 id="使用的案例"><a href="#使用的案例" class="header-anchor">#</a> 使用的案例</h2> <ul><li>创建 mysql5.7
<ul><li><code>docker run -p 33306:3306 -e MYSQL_ROOT_PASSWORD=qq164165005 --name eggmysql -d mysql:5.7</code></li></ul></li></ul> <h2 id="在使用-docker-中遇到的问题-需求"><a href="#在使用-docker-中遇到的问题-需求" class="header-anchor">#</a> 在使用 docker 中遇到的问题&amp;需求</h2> <h3 id="问题"><a href="#问题" class="header-anchor">#</a> 问题</h3> <h4 id="docker-启动时的报错汇总"><a href="#docker-启动时的报错汇总" class="header-anchor">#</a> <a href="https://www.jianshu.com/p/93518610eea1" target="_blank" rel="noopener noreferrer">Docker 启动时的报错汇总<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></h4> <hr> <ul><li>Failed to start Docker Application Container Engine.</li></ul> <p>修改/etc/docker/daemon.json 这个文件</p> <div class="language-dockerfile line-numbers-mode"><pre class="language-dockerfile"><code>&quot;registry-mirrors&quot;: [&quot;https://docker.mirrors.ustc.edu.cn&quot;]
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><hr> <ul><li>docker.service failed.</li></ul> <hr> <ul><li>Failed to restart docker.service: Unit not found.</li></ul> <p><strong>docker.service 启动失败：Unit not found 的原因及解决办法</strong>
删除/usr/lib/systemd/system/docker.service 的[UNIT]里包含的 docker.socket，然后 systemctl daemon-reload，最后 systemctl start docker.service，发现启动成功了。
类似情况
如果是类似的情况，缺少 docker.socket，但是新版本需要 docker.socket。有两种方法可以解决该问题:</p> <ol><li>可以卸载 docker，再重新安装，即可出现 docker.socket。或者</li> <li>创建一个/usr/lib/systemd/system/docker.socket 文件，然后 systemctl
daemon-reload，最后 systemctl start docker.service，即可启动成功。</li></ol> <hr> <h4 id="当构建完镜像启动容器时失败"><a href="#当构建完镜像启动容器时失败" class="header-anchor">#</a> 当构建完镜像启动容器时失败</h4> <ul><li><p>在最后加上 <code>sh -c &quot;while true; do sleep 3600; done&quot;</code></p></li> <li><p>DockerFile 最后加入 <code>ENTRYPOINT [&quot;sh&quot;, &quot;-c&quot;, &quot; exec tail -f /dev/null&quot;]</code>防止容器启动就退出,  用这个命令一直前台阻塞</p></li> <li><p>删除未启动的容器 重新构建镜像</p></li> <li><p>进入容器<code>docker exec [contianerID] -it /bin/sh</code></p></li></ul> <p>查看文件类型 .sh 设置文件类型<code>set ff=&quot;unix&quot;</code></p> <h4 id="permission-denied"><a href="#permission-denied" class="header-anchor">#</a> permission denied</h4> <ul><li><p>sudo docker image ls</p></li> <li><p>sudo goupadd docker</p></li> <li><p>sudo gpasswd -a ${USER} docker</p></li> <li><p>sudo service docker restart</p></li> <li><p>logout 重新登录下</p></li></ul> <h4 id="运行-node-项目-自动退出"><a href="#运行-node-项目-自动退出" class="header-anchor">#</a> 运行 node 项目 自动退出</h4> <p>运行容器的时候      自动进入项目命令行交互界面敲击</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>docker run -it pptr-image bash
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="需求收集"><a href="#需求收集" class="header-anchor">#</a> 需求收集</h3> <h4 id="容器中调用宿主机-docker-命令"><a href="#容器中调用宿主机-docker-命令" class="header-anchor">#</a> 容器中调用宿主机 docker 命令</h4> <p><a href="https://blog.csdn.net/nanhavezhi/article/details/103079056" target="_blank" rel="noopener noreferrer">https://blog.csdn.net/nanhavezhi/article/details/103079056<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>
在构建容器时加上 docker 的目录映射</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token operator">-</span>v <span class="token operator">/</span>run<span class="token operator">/</span>docker<span class="token punctuation">.</span>sock<span class="token operator">:</span><span class="token operator">/</span><span class="token keyword">var</span><span class="token operator">/</span>run<span class="token operator">/</span>docker<span class="token punctuation">.</span>sock  \
<span class="token operator">-</span>v <span class="token operator">/</span>bin<span class="token operator">/</span>docker<span class="token operator">:</span><span class="token operator">/</span>bin<span class="token operator">/</span>docker
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h4 id="在容器外执行某个容器内的某个命令操作"><a href="#在容器外执行某个容器内的某个命令操作" class="header-anchor">#</a> 在容器外执行某个容器内的某个命令操作</h4> <p>命令如下 加上<code>-c $cmd</code></p> <div class="language-dockerfile line-numbers-mode"><pre class="language-dockerfile"><code>sudo docker exec -it $DOCKER_ID /bin/bash -c 'cd /packages/detectron &amp;&amp; python tools/train.py'
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></div></div> <!----> <div class="page-edit"><!----> <!----> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">2021/12/13, 22:52:03</span></div></div> <div class="page-nav-wapper"><div class="page-nav-centre-wrap"><a href="/pages/3eb92e/" class="page-nav-centre page-nav-centre-prev"><div class="tooltip">Docker 学习</div></a> <a href="/pages/daf659111/" class="page-nav-centre page-nav-centre-next"><div class="tooltip">Docker docker-compose笔记</div></a></div> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/pages/3eb92e/" class="prev">Docker 学习</a></span> <span class="next"><a href="/pages/daf659111/">Docker docker-compose笔记</a>→
      </span></p></div></div></div> <div class="article-list"><div class="article-title"><a href="/archives/" class="iconfont icon-bi">最近更新</a></div> <div class="article-wrapper"><dl><dd>01</dd> <dt><a href="/pages/486e07/"><div>
            Git Hooks
            <!----></div></a> <span class="date">02-24</span></dt></dl><dl><dd>02</dd> <dt><a href="/pages/f041f4/"><div>
            标准化编程规范解决方案
            <!----></div></a> <span class="date">02-24</span></dt></dl><dl><dd>03</dd> <dt><a href="/pages/2b68bd/"><div>
            git 提交信息规范
            <!----></div></a> <span class="date">02-24</span></dt></dl> <dl><dd></dd> <dt><a href="/archives/" class="more">更多文章&gt;</a></dt></dl></div></div></main></div> <div class="footer"><div class="icons"><a href="mailto:izhaong@outlook.com" title="发邮件" target="_blank" class="iconfont icon-youjian"></a><a href="https://github.com/izhaong" title="GitHub" target="_blank" class="iconfont icon-github"></a><a href="https://music.163.com/#/playlist?id=755597173" title="听音乐" target="_blank" class="iconfont icon-erji"></a></div> 
  Theme by
  <a href="https://github.com/xugaoyi/vuepress-theme-vdoing" target="_blank" title="本站主题">Vdoing</a> 
    | Copyright © 2021-2022
    <span>izhaong | <a href="https://github.com/izhaong" target="_blank">github</a> | <a href="http://beian.miit.gov.cn/" target="_blank">蜀ICP备2021031194号</a></span></div> <div class="buttons"><div title="返回顶部" class="button blur go-to-top iconfont icon-fanhuidingbu" style="display:none;"></div> <div title="去评论" class="button blur go-to-comment iconfont icon-pinglun" style="display:none;"></div> <div title="主题模式" class="button blur theme-mode-but iconfont icon-zhuti"><ul class="select-box" style="display:none;"><li class="iconfont icon-zidong">
          跟随系统
        </li><li class="iconfont icon-rijianmoshi">
          浅色模式
        </li><li class="iconfont icon-yejianmoshi">
          深色模式
        </li><li class="iconfont icon-yuedu">
          阅读模式
        </li></ul></div></div> <div class="body-bg" style="background:url() center center / cover no-repeat;opacity:0.5;"></div> <!----> <!----></div><div class="global-ui"><div></div></div></div>
    <script src="/assets/js/app.6e09c0e4.js" defer></script><script src="/assets/js/2.26fadcea.js" defer></script><script src="/assets/js/71.39859d24.js" defer></script>
  </body>
</html>
